package com.bw.module_mine.model.Adpter

import android.content.Context
import android.media.MediaMetadataRetriever
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bw.lib_storage.db.entity.History
import com.bw.module_mine.R
import java.io.File
import java.text.SimpleDateFormat
import java.util.Locale

class HistoryDetailAdapter(
    private val context: Context,
    private val historyList: MutableList<History>
) : RecyclerView.Adapter<HistoryDetailAdapter.HistoryDetailViewHolder>() {

    private var onItemClickListener: OnItemClickListener? = null

    interface OnItemClickListener {
        fun onItemClick(history: History)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.onItemClickListener = listener
    }

    class HistoryDetailViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val ivThumbnail: ImageView = itemView.findViewById(R.id.iv_history_thumbnail)
        val tvTitle: TextView = itemView.findViewById(R.id.tv_history_title)
        val tvTime: TextView = itemView.findViewById(R.id.tv_history_time)
        val tvViewCount: TextView = itemView.findViewById(R.id.tv_view_count)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryDetailViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_history_detail, parent, false)
        return HistoryDetailViewHolder(view)
    }

    override fun getItemCount(): Int {
        return historyList.size
    }

    override fun onBindViewHolder(holder: HistoryDetailViewHolder, position: Int) {
        val history = historyList[position]

        // 设置标题
        holder.tvTitle.text = history.title

        // 设置时间
        val timeStr = formatTime(history.queryTime)
        holder.tvTime.text = timeStr

        // 设置视频缩略图

//        setVideoThumbnail(holder.ivThumbnail, history.content)
        Glide.with(context)
            .load(history.image)
            .placeholder(R.drawable.ic_launcher_foreground) // 占位图
            .error(R.drawable.ic_launcher_foreground) // 错误图
            .into(holder.ivThumbnail)
            
        // 设置浏览次数，只有当浏览次数大于1时才显示
        if (history.viewCount > 1) {
            holder.tvViewCount.visibility = View.VISIBLE
            holder.tvViewCount.text = "浏览 ${history.viewCount}次"
        } else {
            holder.tvViewCount.visibility = View.GONE
        }

        // 设置点击事件
        holder.itemView.setOnClickListener {
            onItemClickListener?.onItemClick(history)
        }
    }

    // 格式化时间
    private fun formatTime(date: java.util.Date): String {
        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())
        return sdf.format(date)
    }

    // 从视频路径加载缩略图
    private fun setVideoThumbnail(imageView: ImageView, videoPath: String) {
        try {
            val retriever = MediaMetadataRetriever()
            // 检查是否为本地文件路径
            if (videoPath.startsWith("file://")) {
                retriever.setDataSource(videoPath.substring(7))
            } else if (File(videoPath).exists()) {
                retriever.setDataSource(videoPath)
            } else {
                // 如果是网络路径或文件不存在，尝试直接设置
                retriever.setDataSource(videoPath)
            }

            val bitmap = retriever.frameAtTime
            if (bitmap != null) {
                imageView.setImageBitmap(bitmap)
            }

            retriever.release()
        } catch (e: Exception) {
            e.printStackTrace()
            // 如果获取缩略图失败，保持默认图片
        }
    }
}